// Author: abi
// Project: Xbox 360 Copy of XnaShooter
// Path: C:\code\Xna\XnaShooter\Properties
// Creation date: 02.02.2008 13:39
// Last modified: 06.02.2008 01:25

// Project: XnaShooter, File: GameSettings.cs
// Namespace: XnaShooter.Properties, Class: GameSettings
// Path: C:\code\XnaBook\XnaShooter\Properties, Author: abi
// Code lines: 546, Size of file: 11,61 KB
// Creation date: 07.12.2006 18:22
// Last modified: 07.12.2006 20:13
// Generated with Commenter by abi.exDream.com

#region Using directives
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Threading;
using XnaShooter.Helpers;
#endregion

namespace XnaShooter.Properties
{
	/// <summary>
	/// Game settings, stored in a custom xml file. The reason for this is
	/// we want to be able to store our game data on the Xbox360 too.
	/// On the PC we could just use a Settings/config file and have all the
	/// code autogenerated for us, but this way it works both on PC and Xbox.
	/// Note: The default instance for the game settings is in this class,
	/// this way we get the same behaviour as for normal Settings files!
	/// </summary>
	[Serializable]
	public class GameSettings
	{
		#region Default
		/// <summary>
		/// Filename for our game settings file.
		/// </summary>
		const string SettingsFilename = "XnaShooterSettings.xml";

		/// <summary>
		/// Default instance for our game settings.
		/// </summary>
#if !XBOX360
		[NonSerialized]
#endif
    private static GameSettings defaultInstance = null;

		/// <summary>
		/// Need saving the game settings file? Only set to true if
		/// we really changed some game setting here.
		/// </summary>
#if !XBOX360
		[NonSerialized]
#endif
		private static bool needSave = false;

    /// <summary>
    /// Default
    /// </summary>
    /// <returns>Game settings</returns>
    public static GameSettings Default
		{
      get
			{
        return defaultInstance;
      } // get
    } // Default
		#endregion

		#region Constructor
		/// <summary>
		/// Create game settings, don't allow public constructor!
		/// </summary>
		private GameSettings()
		{
		} // GameSettings()

		/// <summary>
		/// Create game settings. This constructor helps us to only load the
		/// GameSettings once, not again if GameSettings is recreated by
		/// the Deserialization process.
		/// </summary>
		/// <param name="loadSettings">Load settings</param>
		static GameSettings()
		{
			defaultInstance = new GameSettings();
			Load();
		} // GameSettings(loadSettings)
		#endregion

		#region Load
		/// <summary>
		/// Load
		/// </summary>
		public static void Load()
		{
			needSave = false;

			FileStream file =
				FileHelper.LoadGameContentFile(SettingsFilename);
			if (file == null)
				return;

			// If the file is empty, just create a new file with the default
			// settings.
			if (file.Length == 0)
			{
				// Close the file
				file.Close();

				// But first check if there is maybe a file in the game directory
				// to load the default game settings from.
				file = FileHelper.LoadGameContentFile(SettingsFilename);
				if (file != null)
				{
					// Load everything into this class
					GameSettings loadedGameSettings =
						(GameSettings)new XmlSerializer(typeof(GameSettings)).
						Deserialize(file);
					if (loadedGameSettings != null)
						defaultInstance = loadedGameSettings;

					// Close the file
					file.Close();
				} // if (file)

				// Save user settings file
				needSave = true;
				Save();
			} // if (file.Length)
			else
			{
				// Else load everything into this class with help of the
				// XmlSerializer.
				GameSettings loadedGameSettings =
					(GameSettings)new XmlSerializer(typeof(GameSettings)).
					Deserialize(file);
				if (loadedGameSettings != null)
					defaultInstance = loadedGameSettings;

				// Close the file
				file.Close();
			} // else
		} // Load()
		#endregion

		#region Save
		/// <summary>
		/// Save
		/// </summary>
		public static void Save()
		{
			// No need to save if everything is up to date.
			if (needSave == false)
				return;
			
			needSave = false;
			try
			{
				FileStream file = FileHelper.SaveGameContentFile(SettingsFilename);

				// Save everything in this class with help of the XmlSerializer.
				new XmlSerializer(typeof(GameSettings)).
					Serialize(file, defaultInstance);

				// Close the file
				file.Close();
			} // try
			catch
			{
				// Ignore, may fail on the Xbox360!
			} // catch
		} // Save()
		#endregion

		#region Setting variables with properties
		/// <summary>
		/// Highscores
		/// </summary>
		string highscores = "";
		/// <summary>
		/// Highscores
		/// </summary>
		/// <returns>String</returns>
		public string Highscores
		{
			get
			{
				return highscores;
			} // get
			set
			{
				if (highscores != value)
					needSave = true;
				highscores = value;
			} // set
		} // Highscores

		/// <summary>
		/// Player name
		/// </summary>
		string playerName = "Player";
		/// <summary>
		/// Player name
		/// </summary>
		/// <returns>String</returns>
		public string PlayerName
		{
			get
			{
				return playerName;
			} // get
			set
			{
				if (playerName != value)
					needSave = true;
				playerName = value;
			} // set
		} // PlayerName

		/// <summary>
		/// Language
		/// </summary>
		string language = "";
		/// <summary>
		/// Language
		/// </summary>
		/// <returns>String</returns>
		public string Language
		{
			get
			{
				return language;
			} // get
			set
			{
				if (language != value)
					needSave = true;
				language = value;
			} // set
		} // Language

		/// <summary>
		/// BaseGame width
		/// </summary>
		int resolutionWidth = 0;
		/// <summary>
		/// BaseGame width
		/// </summary>
		/// <returns>Int</returns>
		public int ResolutionWidth
		{
			get
			{
				return resolutionWidth;
			} // get
			set
			{
				if (resolutionWidth != value)
					needSave = true;
				resolutionWidth = value;
			} // set
		} // ResolutionWidth

		/// <summary>
		/// BaseGame height
		/// </summary>
		int resolutionHeight = 0;
		/// <summary>
		/// BaseGame height
		/// </summary>
		/// <returns>Int</returns>
		public int ResolutionHeight
		{
			get
			{
				return resolutionHeight;
			} // get
			set
			{
				if (resolutionHeight != value)
					needSave = true;
				resolutionHeight = value;
			} // set
		} // ResolutionHeight

		/// <summary>
		/// Fullscreen
		/// </summary>
		bool fullscreen = true;
		/// <summary>
		/// Fullscreen
		/// </summary>
		/// <returns>Bool</returns>
		public bool Fullscreen
		{
			get
			{
				return fullscreen;
			} // get
			set
			{
				if (fullscreen != value)
					needSave = true;
				fullscreen = value;
			} // set
		} // Fullscreen

		/// <summary>
		/// Performance settings
		/// </summary>
		int performanceSettings = -1;
		/// <summary>
		/// Performance settings
		/// </summary>
		/// <returns>Int</returns>
		public int PerformanceSettings
		{
			get
			{
				return performanceSettings;
			} // get
			set
			{
				if (performanceSettings != value)
					needSave = true;
				performanceSettings = value;
			} // set
		} // PerformanceSettings

		bool postScreenEffects = true;
		/// <summary>
		/// Post screen effects
		/// </summary>
		/// <returns>Bool</returns>
		public bool PostScreenEffects
		{
			get
			{
				return postScreenEffects;
			} // get
			set
			{
				if (postScreenEffects != value)
					needSave = true;
				postScreenEffects = value;
			} // set
		} // PostScreenEffects

		bool shadowMapping = true;
		/// <summary>
		/// ShadowMapping
		/// </summary>
		/// <returns>Bool</returns>
		public bool ShadowMapping
		{
			get
			{
				return shadowMapping;
			} // get
			set
			{
				if (shadowMapping != value)
					needSave = true;
				shadowMapping = value;
			} // set
		} // ShadowMapping

		bool highDetail = true;
		/// <summary>
		/// HighDetail
		/// </summary>
		/// <returns>Bool</returns>
		public bool HighDetail
		{
			get
			{
				return highDetail;
			} // get
			set
			{
				if (highDetail != value)
					needSave = true;
				highDetail = value;
			} // set
		} // HighDetail

		/// <summary>
		/// Sound volume
		/// </summary>
		float soundVolume = 0.8f;
		/// <summary>
		/// Sound volume
		/// </summary>
		/// <returns>Float</returns>
		public float SoundVolume
		{
			get
			{
				return soundVolume;
			} // get
			set
			{
				if (soundVolume != value)
					needSave = true;
				soundVolume = value;
			} // set
		} // SoundVolume

		/// <summary>
		/// Music volume
		/// </summary>
		float musicVolume = 0.6f;
		/// <summary>
		/// Music volume
		/// </summary>
		/// <returns>Float</returns>
		public float MusicVolume
		{
			get
			{
				return musicVolume;
			} // get
			set
			{
				if (musicVolume != value)
					needSave = true;
				musicVolume = value;
			} // set
		} // MusicVolume

		/// <summary>
		/// Music on
		/// </summary>
		bool musicOn = true;
		/// <summary>
		/// Music on
		/// </summary>
		/// <returns>bool</returns>
		public bool MusicOn
		{
			get
			{
				return musicOn;
			} // get
			set
			{
				musicOn = value;
			} // set
		} // MusicOn

		/// <summary>
		/// waitForVSync
		/// </summary>
		bool waitForVSync = false;
		/// <summary>
		/// waitForVSync
		/// </summary>
		/// <returns>bool</returns>
		public bool WaitForVSync
		{
			get
			{
				return waitForVSync;
			} // get
			set
			{
				waitForVSync = value;
			} // set
		} // WaitForVSync

		/// <summary>
		/// Controller sensibility
		/// </summary>
		float controllerSensibility = 1;
		/// <summary>
		/// Controller sensibility
		/// </summary>
		/// <returns>Float</returns>
		public float ControllerSensibility
		{
			get
			{
				return controllerSensibility;
			} // get
			set
			{
				if (controllerSensibility != value)
					needSave = true;
				controllerSensibility = value;
			} // set
		} // ControllerSensibility

		Keys moveLeftKey = Keys.A,
			moveRightKey = Keys.D,
			moveUpKey = Keys.W,
			moveDownKey = Keys.S;//,
			//rollLeftKey = Keys.Q,
			//rollRightKey = Keys.E;

		/// <summary>
		/// Move left key
		/// </summary>
		/// <returns>Char</returns>
		public Keys MoveLeftKey
		{
			get
			{
				return moveLeftKey;
			} // get
			set
			{
				if (moveLeftKey != value)
					needSave = true;
				moveLeftKey = value;
			} // set
		} // MoveLeftKey

		/// <summary>
		/// Move right key
		/// </summary>
		/// <returns>Char</returns>
		public Keys MoveRightKey
		{
			get
			{
				return moveRightKey;
			} // get
			set
			{
				if (moveRightKey != value)
					needSave = true;
				moveRightKey = value;
			} // set
		} // MoveRightKey

		/// <summary>
		/// Move up key
		/// </summary>
		/// <returns>Char</returns>
		public Keys MoveForwardKey
		{
			get
			{
				return moveUpKey;
			} // get
			set
			{
				if (moveUpKey != value)
					needSave = true;
				moveUpKey = value;
			} // set
		} // MoveForwardKey

		/// <summary>
		/// Move down key
		/// </summary>
		/// <returns>Char</returns>
		public Keys MoveBackwardKey
		{
			get
			{
				return moveDownKey;
			} // get
			set
			{
				if (moveDownKey != value)
					needSave = true;
				moveDownKey = value;
			} // set
		} // MoveBackwardKey

		/*obs
		/// <summary>
		/// RollLeftKey
		/// </summary>
		/// <returns>Char</returns>
		public Keys RollLeftKey
		{
			get
			{
				return rollLeftKey;
			} // get
			set
			{
				if (rollLeftKey != value)
					needSave = true;
				rollLeftKey = value;
			} // set
		} // RollLeftKey

		/// <summary>
		/// RollRightKey
		/// </summary>
		/// <returns>Char</returns>
		public Keys RollRightKey
		{
			get
			{
				return rollRightKey;
			} // get
			set
			{
				if (rollRightKey != value)
					needSave = true;
				rollRightKey = value;
			} // set
		} // RollRightKey
		 */
		#endregion

		#region Unit Testing
#if DEBUG
		/// <summary>
		/// Test save game settings
		/// </summary>
		public static void TestSaveGameSettings()
		{
			// Just save some data for the highscores ^^
			GameSettings.Default.Highscores = "TestSaveGameSettings";
			// And save, on the Xbox360 we should get a dialog asking us
			// where we want to save our savegame file.
			GameSettings.Save();
		} // TestSaveGameSettings()
#endif
		#endregion
	} // class GameSettings
} // namespace XnaShooter.Properties
